<template>
    <div class="app-container">
      <el-row :gutter="20">
        <!--用户数据-->
          <!--工具栏-->
        <el-card>
          <div class="head-container">
            <div v-if="crud.props.searchToggle">
              <!-- 搜索 -->
              <el-input v-model="query.keyword" clearable size="small" placeholder="请输入资讯标题" style="width: 200px;"
                class="filter-item" @keyup.enter.native="crud.toQuery" />

  
              <rrOperation />
              <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="add">
                新增
              </el-button>
  
            </div>
            <crudOperation show="" :permission="permission" />
          </div>
        </el-card>
        <el-card style="margin-top:20px;">
          <!--表格渲染-->
          <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;"
            @selection-change="crud.selectionChangeHandler">
            <el-table-column prop="title" label="标题" />
            <el-table-column prop="views" label="浏览量" />
            <el-table-column  label="状态" >
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status == 0" type="success">启用</el-tag>
                <el-tag v-if="scope.row.status == 1" type="danger">停用</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="270">
              <template slot-scope="scope">
                <el-button class="btn" icon="el-icon-edit" type="primary" size="mini" @click="edit(scope.row)"></el-button>
                <el-button class="btn" :loading="scope.row.del_loading" icon="el-icon-delete" type="danger" size="mini" @click="del(scope.row)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
          <!--分页组件-->
          <pagination />
      </el-row>
      <addDialog ref="addDialog" @ok="crud.toQuery"></addDialog>
    </div>
  </template>
  
  <script>
  import crudApi from '@/api/web/news'
  import CRUD, {
    presenter,
    header,
    form,
    crud
  } from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'
  import pagination from '@crud/Pagination'
  import DateRangePicker from '@/components/DateRangePicker'
  import addDialog from './modual/addDialog';
  import majors from '@/utils/majors';
  export default {
    name: 'User',
    components: {
      crudOperation,
      rrOperation,
      udOperation,
      pagination,
      DateRangePicker,
      addDialog
    },
    cruds() {
      return CRUD({
        title: '用户',
        url: 'api/news/art/list',
        methodStr: 'post',
        dataStr: ['data', 'newsList'],
        optShow: {
          reset: false,
          // othoer: true,
        },
        crudMethod: { ...crudApi }
      })
    },
    mixins: [presenter(), header(), crud()],
    data() {
      // 自定义验证
      const validPhone = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入电话号码'))
        } else if (!isvalidPhone(value)) {
          callback(new Error('请输入正确的11位手机号码'))
        } else {
          callback()
        }
      }
      return {
        height: document.documentElement.clientHeight - 180 + 'px;',
        majors,
        selectedOptions: [],
        eduTypes: [
          { label: '专科', value: 1 },
          { label: '本科', value: 2 },
  
        ],
        defaultProps: {
          children: 'children',
          label: 'name',
          isLeaf: 'leaf'
        },
        permission: {
          add: ['admin', 'user:add'],
          edit: ['admin', 'article:edit'],
          del: ['admin', 'user:del']
        },
      }
    },
    mounted: function () {
      const that = this
      window.onresize = function temp() {
        that.height = document.documentElement.clientHeight - 180 + 'px;'
      }
    },
    methods: {
      add() {
        this.$refs.addDialog.add({})
      },
      edit(item) {
        this.$refs.addDialog.edit(item)
      },
      del(item) {
        this.$confirm(`删除后不可恢复，确认删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$set(item, 'del_loading', true)
          crudApi.del({newsId:item.newsId}).then(res => {
            this.$set(item, 'del_loading', false)
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })
              this.crud.toQuery()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        })
  
      },
      changeStatus(item, type, loadingStr) {
        let confirmContent = ''
        let confirmStatus = false
        if (type == 1) {
          if (item.ifRecommend) {
            confirmContent = '取消推荐'
          } else {
            confirmContent = '推荐'
          }
          confirmStatus = !item.ifRecommend
        } else {
          if (item.ifSelected) {
            confirmContent = '取消精选'
          } else {
            confirmContent = '精选'
          }
          confirmStatus = !item.ifSelected
        }
        this.$confirm(`是否${confirmContent}该文章`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$set(item, loadingStr, true)
          crudArticle.changeStatus({ id: item.id, type: type, status: confirmStatus }).then(res => {
            this.$set(item, loadingStr, false)
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.message
              })
              this.crud.toQuery()
            } else {
              this.$message({
                type: 'arror',
                message: res.message
              })
            }
          }).catch(e => {
            this.$set(item, loadingStr, false)
          })
        })
  
      },
      // 禁止输入空格
      keydown(e) {
        if (e.keyCode === 32) {
          e.returnValue = false
        }
      },
      checkboxT(row, rowIndex) {
        return row.id !== this.user.id
      }
    }
  }
  </script>
  
  <style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .vue-treeselect__control,
  ::v-deep .vue-treeselect__placeholder,
  ::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
  
  .n_avatar {
    max-width: 120px;
    max-height: 120px;
  }
  
  .btn {
    margin: 0;
  }
  </style>
  